package com.evosoft.facade;

import com.evosoft.excepciones.CantCreateUpdate;
import com.evosoft.excepciones.NotEntityFoundException;
import com.evosoft.model.Persona;
import com.evosoft.service.ServiceFactory;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.Persistence;

public class PersonaFacade {
	private static PersonaFacade instance;

    private PersonaFacade() {
    }

    public synchronized static PersonaFacade getInstance() {
        if (instance == null) {
            instance = new PersonaFacade();
        }

        return instance;
    }
    
    
    /*Se deberia usar este metodo de fachada para el login y validar la contraseña en vista*/
    public Persona obtenerPorUsuario(String usuario) throws NotEntityFoundException {
    	Persona persona = null;
    	EntityManager em = Persistence.createEntityManagerFactory("bienestarPU").createEntityManager();
        try {
        	
        	persona = ServiceFactory.getInstance().getPersonaService().obtenerPorUsuario(usuario, em);
        	
        } catch (NotEntityFoundException e) {
            throw e;
        } finally {
        	if(em != null) {
				em.clear();
				em.close();
			}
        }    
        return persona;
    } 
    
    /*Id del administrador en sesion que crea el usuario ES requerido*/
    public void crearPersona(Persona persona, Integer id) throws Exception {
        
        EntityManager em = Persistence.createEntityManagerFactory("bienestarPU").createEntityManager();
        try {
        	
        	em.getTransaction().begin();
            
        	ServiceFactory.getInstance().getPersonaService().crearPersona(persona, id, em);
        	
            em.getTransaction().commit();
        } catch (Exception e) {
        	if(em != null) {
        		em.getTransaction().rollback();
        	}
                throw e;
        } finally {
        	if(em != null) {
                    em.clear();
                    em.close();
		}
        }
    }
    
    
    public void actualizarPersona(Persona persona, Integer id) throws Exception {
        EntityManager em = Persistence.createEntityManagerFactory("bienestarPU").createEntityManager();
        try {
        	em.getTransaction().begin();
        	ServiceFactory.getInstance().getPersonaService().actualizarPersona(persona, id, em);
                em.getTransaction().commit();
        } catch (Exception e) {
        	if(em != null) {
        		em.getTransaction().rollback();
        	}
                e.printStackTrace();
                throw e;
        } finally {
        	if(em != null) {
                    em.clear();
                    em.close();
		}
        }
        
    }
    
    
    public void eliminarPersona(Persona persona, Integer id) throws CantCreateUpdate, NotEntityFoundException {
        EntityManager em = Persistence.createEntityManagerFactory("bienestarPU").createEntityManager();
        try {
        	em.getTransaction().begin();
        	ServiceFactory.getInstance().getPersonaService().eliminarPersona(persona, id, em);
                em.getTransaction().commit();
        } catch (CantCreateUpdate e) {
        	if(em != null) {
        		em.getTransaction().rollback();
                }
                throw e;
        } catch (NotEntityFoundException e) {
        	if(em != null) {
        		em.getTransaction().rollback();
                }
                throw e;
        } 
        finally {
        	if(em != null) {
                    em.clear();
                    em.close();
		}
        }
        
    }
    
    
    public Persona obtenerPorId(int id) throws Exception {
    	Persona persona = null;
    	EntityManager em = Persistence.createEntityManagerFactory("bienestarPU").createEntityManager();
        try {
        	
        	persona = ServiceFactory.getInstance().getPersonaService().obtenerPorId(id, em);
        	
        } catch (Exception e) {
            throw e;
        } finally {
        	if(em != null) {
				em.clear();
				em.close();
			}
        }    
        return persona;
    } 
    
    /*Recibe el id de la empresa*/
    public List<Persona> obtenerPersonasPorEmpresa(Integer id)  throws Exception{
        List<Persona> personas = new ArrayList<Persona>();
        EntityManager em = Persistence.createEntityManagerFactory("bienestarPU").createEntityManager();
        try {
        	
        	personas = ServiceFactory.getInstance().getPersonaService().obtenerPorEmpresa(id, em);
        	
        } catch (Exception e) {
            throw e;
        } finally {
        	if(em != null) {
				em.clear();
				em.close();
			}
        }    
        return personas;
        
    }
    
    public List<Persona> obtenerPersonas(int id)  throws Exception{
        List<Persona> personas = new ArrayList<Persona>();
        EntityManager em = Persistence.createEntityManagerFactory("bienestarPU").createEntityManager();
        try {
        	
        	personas = ServiceFactory.getInstance().getPersonaService().obtenerPersonasPorRol(id, em);
        	
        } catch (Exception e) {
            throw e;
        } finally {
        	if(em != null) {
				em.clear();
				em.close();
			}
        }    
        return personas;
        
    }

    
    /*Obtiene las personas de las empresas dependientes al administrador id de la persona en sesión*/
    public List<Persona> obtenerPersonasDeAdmin(int id) throws Exception {
        List<Persona> personas = new ArrayList<Persona>();
        EntityManager em = Persistence.createEntityManagerFactory("bienestarPU").createEntityManager();
        try {
        	personas = ServiceFactory.getInstance().getPersonaService().obtenerPersonasDeAdmin(id, em);
        } catch (Exception e) {
            throw e;
        } finally {
        	if(em != null) {
				em.clear();
				em.close();
		}
        }    
        return personas;
    }
    
}
